﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
using DevComponents;
using System.IO;
using Excel;

namespace QLDSVCNTT_Nhom1
{
    public partial class frmQuanLyDangKyHocPhan : Form
    {
        public frmQuanLyDangKyHocPhan()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();
        public int QuyenHan;
        public string TenDangNhap;
        private void QuanLyDangKyHocPhan_Load(object sender, EventArgs e)
        {
            lblXinChao.Text = "Đang sử dụng tài khoản " + TenDangNhap;
            CapNhatLabel();
            LayDanhHocKy();
            if (QuyenHan == 1)
            {
                QLDQLHP_LayDanhSachLop();
                tiXemDiem.Visible = false;
                tiDKHP.Visible = false;
                tiThietLap.Visible = true;
                tiQLD.Visible = true;
                tiQLHP.Visible = true;
            }
            if (QuyenHan == 3)
            {
                LayDanhSachLop();
                tiXemDiem.Visible = true;
                tiDKHP.Visible = true;
                tiThietLap.Visible = false;
                tiQLD.Visible = false;
                tiQLHP.Visible = false;
            }

            LayDSKieuLoc();
            dtpBatDau.Value = DangKyHocPhan.LayThoiGianBatDauDangKy(HocKyDuocPhepDangKy());
            dtpKetThuc.Value = DangKyHocPhan.LayThoiGianKetThucDangKy(HocKyDuocPhepDangKy());

        }
        public void tabThietLap(int i)
        {
            this.tabControl1.SelectedTabIndex = i;
            panelCotDiem.Visible = false;
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void CapNhatLabel()
        {
            lblTongTCDK.Text = "Tổng tín chỉ đã đăng ký kỳ này: " + DangKyHocPhan.TongSoTinChiDaDangKy(TenDangNhap, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy()) + "(" + TongSoTinChiDangKyMoi() + "tín chỉ đăng ký mới)";
        }

        private void LayDSKieuLoc()
        {
            cbbKieuLoc.Items.Add("Tên học phần");
            cbbKieuLoc.Items.Add("Mã học phần");
            cbbKieuLoc.SelectedIndex = 0;
        }
        private string NamHocDuocPhepDangKy()
        {
            string NamHoc = null;
            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;
            if (HocKyDuocPhepDangKy() == 2)
                NamHoc = (year) + "-" + (year + 1);
            else
                if (HocKyDuocPhepDangKy() == 3)
                    NamHoc = (year - 1) + "-" + (year);
                else
                    NamHoc = (year) + "-" + (year + 1);
            return NamHoc;
        }
        public int HocKyDuocPhepDangKy()
        {
            int month = DateTime.Now.Month;
            if ((month <= 2))//duoc phep dang ky ki 2
            {
                return 2;
            }
            else
            {
                if (month > 2 && month <= 5)//được phép đăng ký kỳ 3
                {
                    return 3;
                }
                else
                    if (month > 5 && month <= 9)// học kỳ 1
                    {
                        return 1;
                    }
            }
            return 0;
        }
        public string NamHocHienTai()
        {
            string NamHoc = null;
            int year = DateTime.Now.Year;
            if (HocKyDuocPhepDangKy() == 1)
                NamHoc = (year - 1) + "-" + (year);
            else
                if (HocKyDuocPhepDangKy() == 3)
                    NamHoc = (year - 1) + "-" + (year);
                else
                    NamHoc = (year) + "-" + (year + 1);
            return NamHoc;
        }
        public int HocKyHienTai()
        {
            int month = DateTime.Now.Month;
            if ((month > 9) || (month <= 2))//hoc ky 1
            {
                return 1;
            }
            else
            {
                if (month > 2 && month <= 7)//hoc ky 2
                {
                    return 2;
                }
                else
                    if (month > 7 && month <= 9)// hoc ky 3
                    {
                        return 3;
                    }
            }
            return 0;
        }
        #region combobox select index change
        private void cbbSinhVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbbQLDKHPSinhVienSelect();
        }

        private void cbbQLDKHPSinhVienSelect()
        {
            if (cbbQLHPSinhVien.SelectedIndex > 0)
            {
                SinhVien sv = (SinhVien)cbbQLHPSinhVien.SelectedItem;
                List<DangKyHocPhan> ds = DangKyHocPhan.XemDKHPTheoSV(sv.MSSV);
                List<string> nam = DangKyHocPhan.LayDanhSachNamDaHoc(sv.MSSV);
                BindListviewQLHP(nam, ds);
            }
            else
                lvDKHP.Items.Clear();
        }
        private void cbbMaMH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbMaMH.SelectedIndex > 0)
            {
                MonHoc mh = (MonHoc)cbbMaMH.SelectedItem;
                cbbTenMH.SelectedIndex = cbbMaMH.SelectedIndex;
                if (QuyenHan == 1)
                {
                    List<DangKyHocPhan> ds = DangKyHocPhan.XemDKHPTheoMonHoc(mh.MaMH);
                }
            }
            else
                lvDKHP.Items.Clear();
        }
        private void cbbTenMH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbTenMH.SelectedIndex > 0)
            {
                MonHoc mh = (MonHoc)cbbTenMH.SelectedItem;
                cbbMaMH.SelectedIndex = cbbTenMH.SelectedIndex;
                List<DangKyHocPhan> ds = DangKyHocPhan.XemDKHPTheoMonHoc(mh.MaMH);
            }
            else
                lvDKHP.Items.Clear();
        }
        private void cbbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ChonLop();
            }
            catch
            {
            }

        }
        private void ChonLop()
        {
            Lop x = (Lop)cbbQLHPLop.SelectedItem;
            LayDSSinhVienTheoLop(x);
            //cbbNamHoc.Items.Clear();
            //LayDSNamHocHocKy(x.MaLop);
        }
        private void cbbNamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            //LayDanhHocKy();
            List<DangKyHocPhan> ds = DangKyHocPhan.XemDKHPTheoSV(TenDangNhap);
            List<string> nam = DangKyHocPhan.LayDanhSachNamDaHoc(TenDangNhap);
            BindListviewQLHP(nam, ds);
        }
        #endregion

        #region Lấy danh sách combobox
        private void QLDQLHP_LayDanhSachLop()
        {
            List<Lop> list = Lop.All();
            cbbQLHPLop.Items.Clear();
            cbbQLHPLop.Items.Insert(0, "<chọn>");
            foreach (Lop item in list)
            {
                cbbQLHPLop.Items.Add(item);
            }
            cbbQLHPLop.DisplayMember = "TenLop";
            cbbQLHPLop.ValueMember = "MaLop";
            cbbQLHPLop.SelectedIndex = 0;


            cbbQLDLop.Items.Clear();
            cbbQLDLop.Items.Insert(0, "<chọn>");
            foreach (Lop item in list)
            {
                cbbQLDLop.Items.Add(item);
            }
            cbbQLDLop.DisplayMember = "TenLop";
            cbbQLDLop.ValueMember = "MaLop";
            cbbQLDLop.SelectedIndex = 0;

        }
        private void LayDSSinhVienTheoLop(Lop lop)
        {
            List<SinhVien> list = SinhVien.LayDSTheoLop(lop.MaLop);
            cbbQLHPSinhVien.Items.Clear();
            cbbQLHPSinhVien.Items.Insert(0, "<chọn>");
            foreach (SinhVien item in list)
            {
                cbbQLHPSinhVien.Items.Add(item);
            }
            cbbQLHPSinhVien.DisplayMember = "MSSV";
            cbbQLHPSinhVien.SelectedIndex = 0;
        }
        private void LayDSMaMonHoc()
        {
            List<MonHoc> list = MonHoc.All();
            cbbMaMH.Items.Clear();
            cbbMaMH.Items.Insert(0, "<chọn>");
            foreach (MonHoc item in list)
            {
                cbbMaMH.Items.Add(item);
            }
            cbbMaMH.DisplayMember = "MaMH";
            cbbMaMH.ValueMember = "MaMH";
            cbbMaMH.SelectedIndex = 0;
        }
        private void LayDSTenMonHoc()
        {
            List<MonHoc> list = MonHoc.All();
            cbbTenMH.Items.Clear();
            cbbTenMH.Items.Insert(0, "<chọn>");
            foreach (MonHoc item in list)
            {
                cbbTenMH.Items.Add(item);
            }
            cbbTenMH.DisplayMember = "TenMH";
            cbbTenMH.ValueMember = "MaMH";
            cbbTenMH.SelectedIndex = 0;
        }
        private void LayDanhSachLop()
        {
            string maLop = SinhVien.LayLopTuMSSV(TenDangNhap);
            int nhom = 0, nam = 0, hk = HocKyHienTai();
            LayNhomVaNam(maLop, hk, ref nhom, ref nam);
            List<Lop> list = Lop.LayDSTheoHeDaoTao(nhom);
            cbbDKHPLop.Items.Clear();
            cbbDKHPLop.Items.Insert(0, "<chọn>");
            foreach (Lop item in list)
            {
                cbbDKHPLop.Items.Add(item);
            }
            cbbDKHPLop.DisplayMember = "TenLop";
            cbbDKHPLop.ValueMember = "MaLop";
            cbbDKHPLop.SelectedIndex = 0;


        }
        private void LayDanhHocKy()
        {
            List<HocKy> list = HocKy.All();
            cbbHocKy.Items.Clear();
            cbbHocKy.Items.Add("");
            foreach (HocKy item in list)
            {
                cbbHocKy.Items.Add(item);
            }
            cbbHocKy.DisplayMember = "TenHK";
            cbbHocKy.ValueMember = "MaHK";
            cbbHocKy.SelectedIndex = 0;
        }
        private void LayDanhSachNamHoc()
        {
            List<string> nam = DangKyHocPhan.LayDanhSachNamDaHoc(TenDangNhap);
            cbbNamHoc.Items.Clear();
            cbbNamHoc.Items.Add("");
            foreach (string i in nam)
            {
                cbbNamHoc.Items.Add(i);
            }
            cbbNamHoc.SelectedIndex = 0;
        }
        #endregion


        #region Bind listview
        private void BindListviewDKHP(List<MonHoc> ds)
        {
            lvDKHP.Items.Clear();
            foreach (MonHoc item in ds)
            {

                ListViewItem lvi = new ListViewItem(item.MaMH);
                lvi.SubItems.Add(item.TenMH);
                lvi.SubItems.Add(item.SoTC.ToString());
                lvi.SubItems.Add(item.TCLT.ToString());
                lvi.SubItems.Add(item.TCTH.ToString());
                lvi.SubItems.Add(item.BatBuoc ? "  x  " : "");
                lvi.SubItems.Add(CType.Tostring(item.MoTaMH));
                lvDKHP.Items.Add(lvi);
            }
        }
        private void BindListviewQLHP(List<string> namhoc, List<DangKyHocPhan> ds)
        {
            lvQLHP.Items.Clear();
            lvQLHP.Groups.Clear();
            ListViewGroup lstGroup;
            //ListViewGroup group1;
            //ListViewGroup group2;
            foreach (string gp in namhoc)
            {
                string temp = "Năm học " + gp;
                lstGroup = new ListViewGroup(temp, HorizontalAlignment.Left);
                lvQLHP.Groups.Add(lstGroup);
                //string hk1 = "Học kỳ 1";
                //string hk2 = "Học kỳ 2";
                //group1= new ListViewGroup(hk1, HorizontalAlignment.Left);
                //group2 = new ListViewGroup(hk2, HorizontalAlignment.Left);
                //lstGroup.Items.Add(group1);
                foreach (DangKyHocPhan item in ds)
                {
                    if (item.NamHoc == gp)
                    {
                        ListViewItem lvi = new ListViewItem(item.MSSV, lstGroup);
                        lvi.SubItems.Add(item.MaMH.ToString());
                        lvi.SubItems.Add(item.MaHK.ToString());
                        lvi.SubItems.Add(item.NamHoc.ToString());
                        lvQLHP.Items.Add(lvi);
                    }
                }
            }
        }
        private void BindListviewXemDiem(List<string> namhoc, List<DangKyHocPhan> ds)
        {
            lvXemDiem.Items.Clear();
            lvXemDiem.Groups.Clear();
            ListViewGroup NamHocGroup;
            foreach (string gp in namhoc)
            {
                string temp = "Năm học " + gp;
                NamHocGroup = new ListViewGroup(temp, HorizontalAlignment.Left);
                lvXemDiem.Groups.Add(NamHocGroup);
                foreach (DangKyHocPhan item in ds)
                {

                    if (item.NamHoc == gp)
                    {
                        float diem1 = QuanLyThi.QuanLyThi_XemDiemThiLan1(TenDangNhap, item.MaMH, item.MaHK, item.NamHoc);
                        MonHoc x = MonHoc.LocMHTheoMa(item.MaMH);
                        ListViewItem lvi = new ListViewItem(x.MaMH, NamHocGroup);
                        lvi.SubItems.Add(x.TenMH.ToString());
                        lvi.SubItems.Add(x.SoTC.ToString());
                        if (diem1 == -1)
                            lvi.SubItems.Add("_");
                        else
                            lvi.SubItems.Add(diem1.ToString());
                        if (diem1 < 4)
                        {
                            float diem2;
                            if (QuanLyThi.QuanLyThi_LayTiLeThiLan2(TenDangNhap, item.MaMH, item.MaHK, item.NamHoc) != 10)
                            {
                                diem2 = QuanLyThi.QuanLyThi_XemDiemThiLan2(TenDangNhap, item.MaMH, item.MaHK, item.NamHoc);
                                if (diem2 == -1)
                                    lvi.SubItems.Add("_");
                                else
                                    lvi.SubItems.Add(diem2.ToString());

                            }
                            else
                            {
                                diem2 = QuanLyThi.QuanLyThi_XemDiemThiLan2_100(TenDangNhap, item.MaMH, item.MaHK, item.NamHoc);
                                if (diem2 == -1)
                                    lvi.SubItems.Add("_");
                                else
                                    lvi.SubItems.Add(diem2.ToString());
                            }
                        }
                        else
                            lvi.SubItems.Add(" ");
                        lvi.SubItems.Add(item.MaHK.ToString());
                        lvXemDiem.Items.Add(lvi);
                    }
                }
            }
        }
        private void BindListviewXemDiemTheoHocKyNamHoc(List<int> hocky, string namhoc, List<DangKyHocPhan> ds)
        {
            lvXemDiem.Items.Clear();
            lvXemDiem.Groups.Clear();
            ListViewGroup NamHocGroup;

            foreach (int hk in hocky)
            {
                string temp = "Học kỳ " + hk;
                NamHocGroup = new ListViewGroup(temp, HorizontalAlignment.Left);
                lvXemDiem.Groups.Add(NamHocGroup);
                foreach (DangKyHocPhan item in ds)
                {

                    if (item.NamHoc == namhoc && item.MaHK == hk)
                    {
                        float diem1 = QuanLyThi.QuanLyThi_XemDiemThiLan1(TenDangNhap, item.MaMH, item.MaHK, item.NamHoc);
                        MonHoc x = MonHoc.LocMHTheoMa(item.MaMH);
                        ListViewItem lvi = new ListViewItem(x.MaMH, NamHocGroup);
                        lvi.SubItems.Add(x.TenMH.ToString());
                        lvi.SubItems.Add(x.SoTC.ToString());
                        lvi.SubItems.Add(diem1.ToString());
                        if (diem1 < 4)
                        {
                            float diem2;
                            if (QuanLyThi.QuanLyThi_LayTiLeThiLan2(TenDangNhap, item.MaMH, item.MaHK, item.NamHoc) != 10)
                            {
                                diem2 = QuanLyThi.QuanLyThi_XemDiemThiLan2(TenDangNhap, item.MaMH, item.MaHK, item.NamHoc);
                                lvi.SubItems.Add(diem2.ToString());

                            }
                            else
                            {
                                diem2 = QuanLyThi.QuanLyThi_XemDiemThiLan2_100(TenDangNhap, item.MaMH, item.MaHK, item.NamHoc);
                                lvi.SubItems.Add(diem2.ToString());
                            }
                        }
                        else
                            lvi.SubItems.Add(" ");
                        lvi.SubItems.Add(item.MaHK.ToString());
                        lvXemDiem.Items.Add(lvi);
                    }
                }
            }
        }
        #endregion

        #region THời gian hợp lệ, số lượng tín chỉ hợp lệ
        public bool ThoiGianHopLe()
        {
            int hk = CType.ToInt32(cbbHocKy.SelectedValue);
            if (DateTime.Now > DangKyHocPhan.LayThoiGianBatDauDangKy(hk))
                return false;
            return true;
        }
        public bool TongSoTinChiHopLe(string maMH, int soTC)
        {
            int TCDKMoi = TongSoTinChiDangKyMoi();
            if (DangKyHocPhan.CoPhaiLaTinChiDangKyMoi(TenDangNhap, maMH, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy()) == 0)
            {

                if ((DangKyHocPhan.TongSoTinChiDaDangKy(TenDangNhap, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy()) + soTC) <= DangKyHocPhan.SoTinChiToiDaHocCaiThien())
                    return true;
            }
            else
            {
                if ((TCDKMoi + soTC) <= DangKyHocPhan.SoTinChiToiDa())
                {
                    return true;
                }
            }
            return false;
        }
        public int TongSoTinChiDangKyMoi()
        {
            int kq = 0;
            List<DangKyHocPhan> list = DangKyHocPhan.CacMonHocDaDangKyKyNay(TenDangNhap, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy());
            foreach (DangKyHocPhan dk in list)
            {
                int dem = DangKyHocPhan.CoPhaiLaTinChiDangKyMoi(dk.MSSV, dk.MaMH, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy());
                if (dem == 1)
                {
                    int sotc = DangKyHocPhan.LayTCMonDangChon(dk.MaMH);
                    kq += sotc;
                }
            }
            return kq;
        }
        #endregion

        #region thêm xóa sửa xem đăng ký học phần
        private void btnDKHPThem_Click(object sender, EventArgs e)
        {
            int x = lvDKHP.CheckedItems.Count;
            if (x > 0)
            {
                string thanhCong = null;
                for (int i = 0; i < x; i++)
                {
                    MonHoc mh = new MonHoc();
                    mh.MaMH = lvDKHP.CheckedItems[i].SubItems[0].Text;
                    mh.TenMH = lvDKHP.CheckedItems[i].SubItems[1].Text;
                    mh.SoTC = CType.ToInt32(lvDKHP.CheckedItems[i].SubItems[2].Text);
                    if ((dtpBatDau.Value > DateTime.Now) || (dtpKetThuc.Value < DateTime.Now))
                    {
                        MessageBox.Show("Hết thời hạn đăng ký!");
                        return;
                    }
                    if (!TongSoTinChiHopLe(mh.MaMH, mh.SoTC))
                    {
                        MessageBox.Show("Vượt quá số tín chỉ cho phép!");
                        CapNhatLabel();
                        return;
                    }
                    try
                    {
                        DangKyHocPhan dk = new DangKyHocPhan(TenDangNhap, mh.MaMH, HocKyDuocPhepDangKy(),
                                                                                     NamHocDuocPhepDangKy());
                        int dkhp = DangKyHocPhan.ThemDKHP(dk);
                        if (dkhp > 0)
                        {
                            thanhCong += mh.TenMH + "; ";
                        }
                    }
                    catch
                    {
                    }

                    //}
                }
                MessageBox.Show("Đã thêm học phần: " + thanhCong);
                CapNhatLabel();
            }
            else
                MessageBox.Show("Vui lòng chọn học phần muốn đang ký!");
        }
        private void btnDKHPXoa_Click(object sender, EventArgs e)
        {
            if (lvDKHP.CheckedItems.Count > 0)
            {
                string hpXoa = null;
                int x = lvDKHP.CheckedItems.Count;
                for (int i = 0; i < x; i++)
                {
                    MonHoc mh = new MonHoc();
                    mh.MaMH = lvDKHP.CheckedItems[i].SubItems[0].Text;
                    mh.TenMH = lvDKHP.CheckedItems[i].SubItems[1].Text;
                    mh.SoTC = CType.ToInt32(lvDKHP.CheckedItems[i].SubItems[2].Text);
                    if ((dtpBatDau.Value > DateTime.Now) || (dtpKetThuc.Value < DateTime.Now))
                    {
                        MessageBox.Show("Hết thời hạn đăng ký!");
                        return;
                    }
                    try
                    {
                        int dkhp = DangKyHocPhan.XoaDKHP(TenDangNhap, mh.MaMH, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy());
                        if (dkhp > 0)
                        {
                            hpXoa += mh.TenMH + "; ";
                        }
                    }
                    catch
                    {
                    }

                }
                if (hpXoa != null)
                    MessageBox.Show("Đã xóa các học phần: " + hpXoa);
                CapNhatLabel();
                XemHPDaDK();
            }
            else
                MessageBox.Show("Chọn học phần muốn xóa");
        }
        private void btnDKHPXem_Click(object sender, EventArgs e)
        {
            XemHPDaDK();
        }
        private void btnLoc_Click(object sender, EventArgs e)
        {
            List<MonHoc> list = new List<MonHoc>();
            if (cbbKieuLoc.SelectedIndex == 0)
            {
                list = MonHoc.LocMHTheoTen(txtGiaTriLoc.Text);

            }
            else
            {
                MonHoc x = MonHoc.LocMHTheoMa(txtGiaTriLoc.Text);
                list.Add(x);
            }
            BindListviewDKHP(list);
        }
        private void XemHPDaDK()
        {
            List<DangKyHocPhan> list = DangKyHocPhan.CacMonHocDaDangKyKyNay(TenDangNhap, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy());
            List<MonHoc> ds = new List<MonHoc>();
            foreach (DangKyHocPhan item in list)
            {
                MonHoc x = MonHoc.TimMHTheoMa(item.MaMH);
                ds.Add(x);
            }
            BindListviewDKHP(ds);
        }
        #endregion






        private void btnSetTime_Click(object sender, EventArgs e)
        {
            if (dtpBatDau.Value < dtpKetThuc.Value)
            {
                int hk = HocKyDuocPhepDangKy();
                int set = DangKyHocPhan.DatThoiGianChoPhepDangKy(hk, dtpBatDau.Value, dtpKetThuc.Value);
                if (set > 0)
                    MessageBox.Show("Đặt thời gian thành công");
            }
            else
                MessageBox.Show("Chọn thời gian không đúng!");

        }


        //==========================tab điểm============================
        private static int QuyDiem(float diem)
        {
            if (diem >= 4 && diem < 5.5)
                return 1;
            if (diem >= 5.5 && diem < 7)
                return 2;
            if (diem >= 7 && diem < 8.5)
                return 3;
            if (diem >= 8.5)
                return 4;
            return 0;
        }
        private void tiQLHP_Click(object sender, EventArgs e)
        {
            lblLop.Visible = true;
            lblMSSV.Visible = true;
            cbbQLHPLop.Visible = true;
            cbbQLHPSinhVien.Visible = true;
            cbbMaMH.Visible = false;
            cbbTenMH.Visible = false;
        }
        private void tiQLHPHocPhan_Click(object sender, EventArgs e)
        {
            lblLop.Visible = false;
            lblMSSV.Visible = false;
            cbbQLHPLop.Visible = false;
            cbbQLHPSinhVien.Visible = false;
            cbbMaMH.Visible = true;
            cbbTenMH.Visible = true;

            LayDSMaMonHoc();
            LayDSTenMonHoc();
        }
        private void tiQLHPSinhVien_Click(object sender, EventArgs e)
        {
            lblLop.Visible = true;
            lblMSSV.Visible = true;
            cbbQLHPLop.Visible = true;
            cbbQLHPSinhVien.Visible = true;
            cbbMaMH.Visible = false;
            cbbTenMH.Visible = false;
        }
        private void tiXemDiem_Click(object sender, EventArgs e)
        {
            LayDanhHocKy();
            LayDanhSachNamHoc();
            lblXD.Text = "Bạn đang sử dụng tài khoản " + TenDangNhap;
            btnAnTichLuy.Visible = false;
        }



        private void btnXemTatCa_Click(object sender, EventArgs e)
        {
            List<DangKyHocPhan> ds = DangKyHocPhan.XemDKHPTheoSV(TenDangNhap);
            int count = cbbNamHoc.Items.Count;
            List<string> nam = new List<string>();
            for (int i = 0; i < count; i++)
                nam.Add(cbbNamHoc.Items[i].ToString());
            BindListviewXemDiem(nam, ds);
        }

        private void btnLocDiem_Click(object sender, EventArgs e)
        {
            if (cbbNamHoc.SelectedIndex != 0)
            {
                List<DangKyHocPhan> ds = DangKyHocPhan.XemDKHPTheoSV(TenDangNhap);
                string nam = cbbNamHoc.SelectedItem.ToString();
                List<HocKy> temp = HocKy.All();
                List<int> hk = new List<int>();

                if (cbbHocKy.SelectedIndex == 0)
                    foreach (HocKy item in temp)
                    {
                        hk.Add(item.MaHK);
                    }
                else
                {
                    HocKy chon = (HocKy)cbbHocKy.SelectedItem;
                    hk.Add(chon.MaHK);
                }
                BindListviewXemDiemTheoHocKyNamHoc(hk, nam, ds);

            }
        }

        private void TichLuyHocKy_Click(object sender, EventArgs e)
        {
            try
            {
                float TinChiTichLuy = 0;
                float TBTichLuy = 0;
                string test = lvXemDiem.SelectedItems[0].Group.Header;
                int hk = int.Parse(lvXemDiem.SelectedItems[0].SubItems[5].Text);
                string namhoc = test.Substring(8);
                List<DangKyHocPhan> dkhp = DangKyHocPhan.CacMonHocDaDangKyKyNay(TenDangNhap, hk, namhoc);
                TrungBinhTichLuyHocKy(ref TinChiTichLuy, ref TBTichLuy, dkhp);
                MessageBox.Show("Trung bình tích lũy: " + TBTichLuy.ToString("F2") + "(trên tổng số " + TinChiTichLuy + "tín chỉ)");
            }
            catch
            {

            }


        }
        private void TrungBinhTichLuyHocKy(ref float TinChiTichLuy, ref float TBTichLuy, List<DangKyHocPhan> dkhp)
        {
            float tam = 0;
            float diemMAX = 0;
            float tichLuy = 0;
            int soTC = 0;
            foreach (DangKyHocPhan dk in dkhp)
            {
                tam = QuanLyThi.QuanLyThi_XemDiemThiLan1(TenDangNhap, dk.MaMH, dk.MaHK, dk.NamHoc);
                if (tam < 4)
                    if (QuanLyThi.QuanLyThi_LayTiLeThiLan2(TenDangNhap, dk.MaMH, dk.MaHK, dk.NamHoc) != 10)
                        tam = QuanLyThi.QuanLyThi_XemDiemThiLan2(TenDangNhap, dk.MaMH, dk.MaHK, dk.NamHoc);
                    else
                        tam = QuanLyThi.QuanLyThi_XemDiemThiLan2_100(TenDangNhap, dk.MaMH, dk.MaHK, dk.NamHoc);
                if (tam > diemMAX)
                    diemMAX = tam;
                if (diemMAX >= 4)
                {
                    soTC = DangKyHocPhan.LayTCMonDangChon(dk.MaMH);
                    tichLuy += QuyDiem(diemMAX) * soTC;
                    TinChiTichLuy += soTC;
                }

            }
            TBTichLuy = tichLuy / TinChiTichLuy;
        }
        private void btnHienTichLuy_Click(object sender, EventArgs e)
        {
            btnHienTichLuy.Visible = false;
            btnAnTichLuy.Visible = true;
            lblXemTichLuy.Visible = true;
            float TinChiTichLuy = 0;
            float TBTichLuy = 0;
            List<string> dkhpMa = DangKyHocPhan.LayDSMaCacHocPhanDaHoc(TenDangNhap);
            TinhChiTichLuyVaTrungBinhTichLuy(ref TinChiTichLuy, ref TBTichLuy, dkhpMa);
            lblXemTichLuy.Text = "Trung bình tích lũy: " + TBTichLuy.ToString("F2") + "(trên tổng số " + TinChiTichLuy + "tín chỉ)";
        }

        private void TinhChiTichLuyVaTrungBinhTichLuy(ref float TinChiTichLuy, ref float TBTichLuy, List<string> dkhpMa)
        {
            float tam = 0;
            float diemMAX = 0;
            float tichLuy = 0;
            int soTC = 0;
            foreach (string Ma in dkhpMa)
            {
                List<DangKyHocPhan> cungMaMH = DangKyHocPhan.LayDSCacDKHPCungMaMH(TenDangNhap, Ma);
                foreach (DangKyHocPhan dk in cungMaMH)
                {
                    tam = QuanLyThi.QuanLyThi_XemDiemThiLan1(TenDangNhap, dk.MaMH, dk.MaHK, dk.NamHoc);
                    if (tam < 4)
                        if (QuanLyThi.QuanLyThi_LayTiLeThiLan2(TenDangNhap, dk.MaMH, dk.MaHK, dk.NamHoc) != 10)
                            tam = QuanLyThi.QuanLyThi_XemDiemThiLan2(TenDangNhap, dk.MaMH, dk.MaHK, dk.NamHoc);
                        else
                            tam = QuanLyThi.QuanLyThi_XemDiemThiLan2_100(TenDangNhap, dk.MaMH, dk.MaHK, dk.NamHoc);
                    if (tam > diemMAX)
                        diemMAX = tam;
                }
                if (diemMAX >= 4)
                {
                    soTC = DangKyHocPhan.LayTCMonDangChon(Ma);
                    tichLuy += QuyDiem(diemMAX) * soTC;
                    TinChiTichLuy += soTC;
                }

            }
            TBTichLuy = tichLuy / TinChiTichLuy;
        }

        private void btnAnTichLuy_Click(object sender, EventArgs e)
        {
            btnHienTichLuy.Visible = true;
            btnAnTichLuy.Visible = false;
            lblXemTichLuy.Visible = false;
        }
        //======================quản lý điểm===================================
        private void cbbQLDMaMH_SelectedIndexChanged(object sender, EventArgs e)
        {
            MonHoc mh = (MonHoc)cbbMaMH.SelectedItem;
            DataTable dt = DangKyHocPhan.LayDSSinhVienTheoMonHoc(mh.MaMH, HocKyHienTai(), NamHocHienTai());
            DataTable newTable = new DataTable();
            newTable.Columns.Add("MSSV", typeof(string));
            newTable.Columns.Add("Mã môn học", typeof(string));
            newTable.Columns.Add("Tên môn học", typeof(string));
            DataTable cotDiem = DangKyHocPhan.QLD_LayDanhSachCotDiem(mh.MaMH, HocKyHienTai(), NamHocHienTai());
            int count = 0;
            List<int> MaD = new List<int>();
            for (int j = 0; j < cotDiem.Rows.Count; j++)
            {
                MaD.Add(int.Parse(cotDiem.Rows[j][0].ToString()));
                string namecol = cotDiem.Rows[j][1] + "0 % (" + (count++) + ")";
                newTable.Columns.Add(namecol, typeof(string));

            }
            foreach (DataRow row in dt.Rows)
            {
                DataRow dr = newTable.NewRow();
                dr[0] = row[0].ToString();
                dr[1] = row[1].ToString();
                dr[2] = row[2].ToString();
                int i = 3;
                foreach (int ma in MaD)
                {
                    int d = DangKyHocPhan.QLD_LayDiemCotThuI(ma, row[0].ToString());
                    if (d == -1)
                        dr[i++] = "";
                    else
                        dr[i++] = d;
                }
                newTable.Rows.Add(dr);
            }
            dgvQLD.DataSource = newTable;
        }

        private void cbbQLDTenMH_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbbQLDLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbQLDLop.SelectedIndex > 0)
            {
                Lop l = (Lop)cbbQLDLop.SelectedItem;
                List<MonHoc> lst = new List<MonHoc>();
                if (QuyenHan == 1)
                {
                    lst = MonHoc.MonHoc_LayDSMonHocTheoLop(l.MaLop, HocKyHienTai(), NamHocHienTai());
                }
                else
                    lst = MonHoc.MonHoc_LayDSMonHocTheoGV(TenDangNhap, HocKyHienTai(), NamHocHienTai());
                cbbMaMH.DataSource = lst;
                cbbMaMH.DisplayMember = "MaMH";
                cbbMaMH.ValueMember = "MaMH";
                cbbTenMH.DataSource = lst;
                cbbTenMH.DisplayMember = "TenMH";
                cbbTenMH.ValueMember = "MaMH";
            }
        }

        //======================tab thiết lập
        private void tiThietLap_Click(object sender, EventArgs e)
        {
            dgvKeHoachDaoTao.Visible = false;
            panelKeHoachDaoTao.Visible = false;
            cbbChonBang.Visible = false;

        }
        DataSet dsSource = null;//Biến toàn cục chứa DataSet đã Load
        private void btnReadxls_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "*.xls|*.xls";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                dsSource = GetDatasetFromExcel(dlg.FileName);
                LoadComboBox(dsSource);//Hiển thị danh sách các TableName lên Combobox
                txtFileName.Text = dlg.FileName;
                DataTable dt = dsSource.Tables[cbbChonBang.Text];
                dgvKeHoachDaoTao.DataSource = dt;

                cbbChonBang.Visible = true;
                panelKeHoachDaoTao.Visible = true;
                dgvKeHoachDaoTao.Visible = true;
            }

        }

        void LoadComboBox(DataSet ds)
        {
            foreach (DataTable tbl in ds.Tables)
            {
                cbbChonBang.Items.Add(tbl.TableName);
            }
            cbbChonBang.SelectedIndex = 0;

        }
        DataSet GetDatasetFromExcel(string path)
        {
            FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

            excelReader.IsFirstRowAsColumnNames = true;
            DataSet result = excelReader.AsDataSet();

            return result;

        }

        private void cbbChonBang_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt = dsSource.Tables[cbbChonBang.Text];
            dgvKeHoachDaoTao.DataSource = dt;
        }

        private void btnThemKHDT_Click(object sender, EventArgs e)
        {
            dgvKeHoachDaoTao.DataSource = null;
            dt = new DataTable();
            dt = dsSource.Tables[cbbChonBang.Text];
            int count = dt.Rows.Count;
            for (int i = 0; i < count - 1; i++)
            {
                KeHoachDaoTao.KeHoachDaoTao_Them(int.Parse(dt.Rows[i][0].ToString()), dt.Rows[i][1].ToString(), int.Parse(dt.Rows[i][3].ToString()), int.Parse(dt.Rows[i][4].ToString()), NamHocDuocPhepDangKy(), i);
            }
            MessageBox.Show("Đã thêm!");
        }

        private void cbbDKHPKeHoachDaoTao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbDKHPLop.SelectedIndex > 0)
            {
                Lop l = (Lop)cbbDKHPLop.SelectedItem;
                int nhom = 0, nam = 0;
                int hk = HocKyDuocPhepDangKy();
                LayNhomVaNam(l.MaLop, hk, ref nhom, ref nam);

                List<MonHoc> list = MonHoc.MonHoc_XemTheoKeHoachDaoTao(nhom, nam, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy());
                BindListviewDKHP(list);
            }
        }

        private static void LayNhomVaNam(string MaLop, int hk, ref int nhom, ref int Nam)
        {
            int temp = CType.ToInt32(MaLop.Substring(3, 2));
            int namBatDau = temp + 1976;

            Nam = DateTime.Now.Year - namBatDau;
            if (hk == 1)
                Nam += 1;
            nhom = 0;
            if (MaLop.Count() == 5)
            {
                nhom = 1;

            }
            else
                if (MaLop.Substring(5, 2) == "CD")
                {
                    nhom = 2;
                }

                else
                {
                    nhom = 3;
                }
        }

        private void btnThietLapSoTC_Click(object sender, EventArgs e)
        {
            if (txtSoTCMAX.Text != "")
            {
                DangKyHocPhan.QuanLyDangKyHocPhan_DatSoTC(1, int.Parse(txtSoTCMAX.Text));
                MessageBox.Show("Đã đặt số tín chỉ tối đa");
            }
            if (txtSoTCMAXCaiThien.Text != "")
            {
                DangKyHocPhan.QuanLyDangKyHocPhan_DatSoTC(2, int.Parse(txtSoTCMAXCaiThien.Text));
                MessageBox.Show("Đã đặt số tín chỉ tối đa học cải thiện");

            }
        }

        private void btnQLDCapNhat_Click(object sender, EventArgs e)
        {
            DataTable cotDiem = DangKyHocPhan.QLD_LayDanhSachCotDiem(dgvQLD.Rows[0].Cells[1].Value.ToString(), HocKyHienTai(), NamHocHienTai());
            for (int i = 0; i < dgvQLD.RowCount - 1; i++)
            {
                for (int j = 0; j < cotDiem.Rows.Count; j++)
                {
                    try
                    {
                        if (dgvQLD.Rows[i].Cells[j + 3].Value == DBNull.Value)
                        {
                            QuanLyThi.QuanLyThi_CapNhatDiem(dgvQLD.Rows[i].Cells[0].Value.ToString(),
                                                             int.Parse(cotDiem.Rows[j][0].ToString()),
                                                             -1);
                        }
                        else
                            if (int.Parse(dgvQLD.Rows[i].Cells[j + 3].Value.ToString()) >= 0 && int.Parse(dgvQLD.Rows[i].Cells[j + 3].Value.ToString()) <= 10)
                                QuanLyThi.QuanLyThi_CapNhatDiem(dgvQLD.Rows[i].Cells[0].Value.ToString(),
                                                                     int.Parse(cotDiem.Rows[j][0].ToString()),
                                                                     int.Parse(dgvQLD.Rows[i].Cells[j + 3].Value.ToString()));
                            else
                            {
                                MessageBox.Show("Giá trị điểm không hợp lệ!");
                            }
                    }
                    catch
                    {
                    }

                }

            }
        }

        private void btnQLHPXoa_Click(object sender, EventArgs e)
        {
            if (lvQLHP.CheckedItems.Count > 0)
            {
                string hpXoa = null;
                int x = lvQLHP.CheckedItems.Count;
                for (int i = 0; i < x; i++)
                {
                    try
                    {
                        int dkhp = DangKyHocPhan.XoaDKHP(lvQLHP.CheckedItems[i].SubItems[0].Text, lvQLHP.CheckedItems[i].SubItems[1].Text, HocKyDuocPhepDangKy(), NamHocDuocPhepDangKy());
                        if (dkhp > 0)
                        {
                            hpXoa += lvQLHP.CheckedItems[i].SubItems[1].Text + "; ";
                        }
                        else
                            MessageBox.Show("Đã hết thời hạn chỉnh sửa của kỳ này!");

                    }
                    catch
                    {

                    }

                }
                if (hpXoa != null)
                {
                    MessageBox.Show("Đã xóa học phần: " + hpXoa);
                    cbbQLDKHPSinhVienSelect();
                }
            }
            else
                MessageBox.Show("Chọn học phần muốn xóa");
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            //DangKyHocPhan.
        }


        private void btnCotDiem_Click(object sender, EventArgs e)
        {
            panelCotDiem.Visible = true;
        }

        private void tiQLD_Click(object sender, EventArgs e)
        {
            panelCotDiem.Visible = false;
        }

        private void btnXong_Click(object sender, EventArgs e)
        {
            //try
            //{
            int tile = 0, phantramThi = 0, phantramKT = 0, phantramThiLai = 0;
            MonHoc mh = (MonHoc)cbbMaMH.SelectedItem;
            int x = dgvCotDiem.Rows.Count;
            for (int i = 0; i < x - 1; i++)
            {
                if (i < x - 3)
                    phantramKT += int.Parse(dgvCotDiem.Rows[i].Cells[0].Value.ToString());
                else

                    if (i == x - 3)
                        phantramThi += int.Parse(dgvCotDiem.Rows[i].Cells[0].Value.ToString());
                    else
                        phantramThiLai += int.Parse(dgvCotDiem.Rows[i].Cells[0].Value.ToString());

            }
            if (((phantramKT + phantramThi) != 100) || ((phantramKT + phantramThiLai) != 100))
            {
                MessageBox.Show("Tổng khác 100% !");
                return;
            }
            for (int i = 0; i < x; i++)
            {
                int MaD = -1;
                tile = int.Parse(dgvCotDiem.Rows[i].Cells[0].Value.ToString().Substring(0, 1));
                if (i == x - 1)
                    MaD = DangKyHocPhan.ThemCotDiem(mh.MaMH, 3, tile, HocKyHienTai(), NamHocHienTai(), i);
                else
                    if (i == x - 2)
                        MaD = DangKyHocPhan.ThemCotDiem(mh.MaMH, 2, tile, HocKyHienTai(), NamHocHienTai(), i);
                    else
                        MaD = DangKyHocPhan.ThemCotDiem(mh.MaMH, 1, tile, HocKyHienTai(), NamHocHienTai(), i);
                List<string> lst = SinhVien.LayDSTheoMonHoc(mh.MaMH, HocKyHienTai(), NamHocHienTai());
                foreach (string item in lst)
                {
                    DangKyHocPhan.ThemQuanLyDiem(item, MaD);
                }
            }
            //}
            //catch (Exception)
            //{

            //    MessageBox.Show("Vui lòng nhập đúng giá trị!");
            //}




            panelCotDiem.Visible = false;
        }













    }
}
